﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Common;
using System.IO;

namespace MainFrom
{
    public partial class InitSystemForm : Form
    {
        public InitSystemForm()
        {
            InitializeComponent();
        }

        private void InitSystemForm_Load(object sender, EventArgs e)
        {
            List<object> dataSource = new List<object>();
            dataSource.Add(new { Text = "MSSQLSERVER", Value = "." });
            dataSource.Add(new { Text = "SQLEXPRESS", Value = @".\sqlexpress" });
            cmpServer.DataSource = dataSource;
            cmpServer.ValueMember = "Value";
            cmpServer.DisplayMember = "Text";
            cmpServer.SelectedIndex = 1;
            txt_DataBaseName.Text = "OreSystemData";
            txtDataBaseUser.Text = "sa";
            txtDataBasePW.PasswordChar = '*';
            txtDataBasePW.Text = "000000";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string winConnStr = string.Format(@"Server={0};Database=master;Trusted_Connection=True;", cmpServer.SelectedValue.ToString()); ;
            string constrMaster = string.Format("Data Source={0}; Initial Catalog={1};User ID={2};Password={3}", cmpServer.SelectedValue.ToString(),"master", txtDataBaseUser.Text, txtDataBasePW.Text);
            string connStr = string.Format("server={0};database={1};uid={2};pwd={3};", cmpServer.SelectedValue.ToString(), txt_DataBaseName.Text, txtDataBaseUser.Text, txtDataBasePW.Text);

            if (ToolSql.ExistSqlServerService())
            {
                //MessageBox.Show("数据库已安装");
            }
            else
            {
                MessageBox.Show("没有安装数据库,请先安装数据库");
                return;
            }
            try
            {
                string sql = string.Format(@"ALTER LOGIN sa ENABLE;
ALTER LOGIN {0} WITH PASSWORD = '{1}';",txtDataBaseUser.Text, txtDataBasePW.Text);
                 if (MsSqlHelper.openConn(winConnStr))
                {
                    //修改sa密码
                    MsSqlHelper.ExceuteNoQueryByConnStr(sql, winConnStr, null);
                    AppConfigTool.UpdateConnectionStringsConfig("mssql", winConnStr, "");
                    AppConfigTool.UpdateAppConfig("mssql", winConnStr, "");
                    string configPath = @"Config/Hib.cfg.xml";
                    string xmlFile = Path.Combine(Application.StartupPath, configPath);
                    AppConfigTool.SetNHConfig(connStr, xmlFile);
                    string dataPath = @"D:\OreSystemData\BackData";
                    DirectoryTool.CreateDir(dataPath);
                    string decDataBaseFile = Path.Combine(@"D:",@"\OreSystemData\OreSystemData.mdf") ;
                    string decDataBaseLogFile = Path.Combine(@"D:", @"\OreSystemData\OreSystemData_log.ldf");
                    string srcDir = Path.Combine(Application.StartupPath + @"\OreSystemData");
                    string DataBaseFile = Path.Combine(Application.StartupPath + @"\OreSystemData\OreSystemData.mdf");
                    string DataBaseLogFile = Path.Combine(Application.StartupPath + @"\OreSystemData\OreSystemData_log.ldf");
                    if (File.Exists(DataBaseFile)&&File.Exists(DataBaseLogFile))
                    {
                        if (!File.Exists(decDataBaseFile) && !File.Exists(decDataBaseLogFile))
                        {
                            File.Move(DataBaseFile, decDataBaseFile);
                            File.Move(DataBaseLogFile, decDataBaseLogFile);
                        }
                    }
                    
                    //DirectoryTool.MoveDir(srcDir, dataPath);

                    MessageBox.Show("初始化配置成功");
                }
                else
                {
                    MessageBox.Show("连接失败！");
                }
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
            
        }
    }
}
